<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="c1" width="600" height="400"></canvas>
    <a href="https://article.itxueyuan.com/BJnKL3" target="_blank">参考文档</a>
    <script>
        /** @type {HTMLCanvasElement} */
        // 画布
        let c1 = document.getElementById("c1")
        // 画笔
        let ctx = c1.getContext("2d")
        // 合成效果
        
        // 目标图像destination
        ctx.fillStyle = "rgba(0,0,255)"
        ctx.fillRect(300, 200, 100, 100)

        // 目标图形和源图形重叠且都不透明的部分才被绘制
        // ctx.globalCompositeOperation = "source-in"

        // 目标图形和源图形不重叠的部分会被绘制
        ctx.globalCompositeOperation = "source-out"
        
        // 源图像source
        ctx.fillStyle = "rgba(0,255,0)"
        ctx.fillRect(250, 150, 100, 100)
    </script>
</body>

</html>